import random


def start():
    """初始顺序，返回元素为字符串的列表"""
    cards_start = [i + j for i in desigh for j in num] + ghost
    return cards_start


def shuffle_cards(cards_start):
    """打乱顺序，返回元素为字符串的列表"""
    random.shuffle(cards_start)
    return cards_start


def traver(cards_shuffle, m):
    """发牌给m个人,返回二维列表"""
    person = []
    for i in range(m):
        person.append(cards_shuffle[i::m])
    return person


def sort_cards(person, m):
    """对m个人手上的牌进行升序排序，花色按黑红梅方，牌面按点数，大王最大，小王第二大"""
    person_sort = []
    for i in range(m):
        if ('jokers' in person[i]) and ('JOKERS' in person[i]):
            person[i].remove('jokers')
            person[i].remove('JOKERS')
            person_sort.append(sorted(person[i], key=lambda x: (desigh.index(x[0]), num.index(x[1:])))+['jokers', 'JOKERS'])
        elif 'jokers' in person[i]:
            person[i].remove('jokers')
            person_sort.append(sorted(person[i], key=lambda x: (desigh.index(x[0]), num.index(x[1:])))+['jokers'])
        elif 'JOKERS' in person[i]:
            person[i].remove('JOKERS')
            person_sort.append(sorted(person[i], key=lambda x: (desigh.index(x[0]), num.index(x[1:])))+['JOKERS'])
        else:
            person_sort.append(sorted(person[i], key=lambda x: (desigh.index(x[0]), num.index(x[1:]))))
    return person_sort

def sort_cards2(person, m, cards):
    """对m个人手上的牌进行升序排序，花色按黑红梅方，牌面按点数，大王最大，小王第二大"""
    person_sort = []
    for i in range(m):
        person_sort.append(sorted(person[i],key=lambda x: cards.index(x)))
    return person_sort


if __name__ == '__main__':
    desigh = ['♠', '♥', '♣', '♦']  # 表示黑桃、红桃、梅花、方块
    num = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
    ghost = ['jokers', 'JOKERS']
    n = int(input())  # 输入参与游戏的人数
    s = int(input())
    random.seed(s)
    print(f'参与游戏的人数：{n}')
    cards = start()
    print('新牌顺序')
    print(*cards)
    cards_after = shuffle_cards(cards)
    print('洗牌顺序')
    print(*cards_after)
    cards_n = traver(cards_after, n)
    cards_n2 = [sublist[:] for sublist in cards_n]
    print('每个人手上分到的牌')
    for i in range(n):
        print(*cards_n[i])
    cards_sort = sort_cards(cards_n, n)
    cards_sort2 = sort_cards2(cards_n2, n, start())

    print('排序1：每个人手上排序的牌')
    for i in range(n):
        print(*cards_sort[i])
    print('排序2：每个人手上排序的牌')
    for i in range(n):
        print(*cards_sort2[i])

    print(f'两种排序结果是否相同: {cards_sort2==cards_sort}')
